﻿using System;
using Castle.MonoRail.Framework;
using GettingStartedSample.Models;

namespace GettingStartedSample.Controllers
{
    [Layout("default"), Rescue("generalerror")]
    public class VenueController : SmartDispatcherController
    {
        public void New()
        {
        }

        public void Delete(int id)
        {
            Venue venue = Venue.FindById(id);
            venue.Delete();
            RedirectToAction("list");
        }

        public void Create([DataBind("venue")] Venue venue)
        {
            venue.Create();
            RedirectToAction("list");
        }

        public void List()
        {
            PropertyBag["venues"] = Venue.FindAll();
        }

        public void Update(int id)
        {
            PropertyBag["venue"] = Venue.FindById(id);
            RenderView("new");
        }

        public void Update([DataBind("venue")] Venue venue)
        {
            try
            {
                venue.Update();
                RedirectToAction("list");
            }
            catch (Exception ex)
            {
                Flash["error"] = ex.Message;
                Flash["venue"] = venue;

                RedirectToAction("edit", "venue.id=" + venue.Id);
            }
        }
    }
}